/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ArticlesSortContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.BaseArticleGroup;
import com.xteam.ecsystem.service.admin.ArticlesSortService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 文章分类控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class ArticlesSortContorller {

	@Autowired
	private ArticlesSortService asService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	@RequestMapping("/delBagByIds.do")
	public void delBagByIds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bagIds = ReqHelper.getRequestString(request, "bagIds");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.asService.delObject(bagIds);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTGROUP_MGR, 
				"delObject", ConstantLocation.LOG_DELETE, "BaseArticleGroup");

	}
	
	@RequestMapping("/getBasInfoList.do")
	public void getBasInfoList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bagName = ReqHelper.getRequestString(request, "bagName");
		
		String strList = this.asService.getBasInfoList(pageIndex, pageSize, bagName);
		response.getWriter().write(strList);
	}
	
	@RequestMapping("/saveOrUpdateBasInfo.do")
	public void saveOrUpdateBasInfo(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "basInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		BaseArticleGroup baGroup = (BaseArticleGroup)JSONObject.toBean(obj, BaseArticleGroup.class);
		
		if(StringUtil.isNotNullOrEmpty(baGroup.getBagId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTGROUP_MGR, 
				"saveOrUpdateObject", lslType, "BaseArticleGroup");
		
		this.asService.saveOrUpdateObject(baGroup, baGroup.getBagId());
	}
	
	@RequestMapping("/getBasInfoById.do")
	public void getBasInfoById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bagId = ReqHelper.getRequestString(request, "bagId");		
		String strInfo = this.asService.getBasInfoById(bagId);
		
		response.getWriter().write(strInfo);
	}
	
	@RequestMapping("/updateBasStateById.do")
	public void updateBasStateById(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String bagId = ReqHelper.getRequestString(request, "bagId");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");

		this.asService.updateBasStateById(bagId);
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_ARTGROUP_MGR, 
				"updateBasStateById", ConstantLocation.LOG_Edit_STATE, "BaseArticleGroup");

	}
	
	@RequestMapping("/getBasTreeGrid.do")
	public void getBasTreeGrid(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		int pageIndex = ReqHelper.getRequestInt(request, "pageIndex");
		int pageSize = ReqHelper.getRequestInt(request, "pageSize");
		String bagName = ReqHelper.getRequestString(request, "bagName");
		
		String basTreeGrid = this.asService.getBasTreeGrid(request,
				pageIndex, pageSize, bagName);
		response.getWriter().write(basTreeGrid);
	}
	
	@RequestMapping("/lazyLoadBasTree.do")
	public void lazyLoadBasTree(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		//此处传递的id充当下一级的pid
		String bagParent = ReqHelper.getRequestString(request, "bagId");

		String bggTree = this.asService.lazyLoadBasTree(bagParent);
		response.getWriter().write(bggTree);

	}
	
	@RequestMapping("/lazyLoadBasTreeGrid.do")
	public void lazyLoadBasTreeGrid(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		//此处传递的id充当下一级的pid
		String bagParent = ReqHelper.getRequestString(request, "bagId");

		String bggTree = this.asService.lazyLoadBasTreeGrid(bagParent);
		response.getWriter().write(bggTree);

	}
	
	@RequestMapping("/getBaiByBas.do")
	public void getBaiByBas(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		String bagIds = ReqHelper.getRequestString(request, "bagIds");

		boolean isUsed = this.asService.getBaiByBas(bagIds);
		if (isUsed) {
			response.getWriter().write("1");
		}else{
			response.getWriter().write("0");
		}
	}
	
}
